/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "queue.h"
#include "string.h"
#include "configuration.h"
#include "Mytask.h"
#include "semphr.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
QueueHandle_t xOLEDQueue = NULL;
QueueHandle_t xGripperQueue = NULL;
osEventFlagsId_t evt_id_Serial = NULL;   // 给串口任务用
osEventFlagsId_t evt_id_LED = NULL;      
osEventFlagsId_t evt_id_Servo_GR = NULL;
osEventFlagsId_t evt_id_OLED = NULL;
// 添加舵机控制互斥�??
SemaphoreHandle_t xServoMutex = NULL;
extern RobotAxis Button_select_state;
/* USER CODE END Variables */
/* Definitions for servo_control */
osThreadId_t servo_controlHandle;
const osThreadAttr_t servo_control_attributes = {
  .name = "servo_control",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for oled_show */
osThreadId_t oled_showHandle;
const osThreadAttr_t oled_show_attributes = {
  .name = "oled_show",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for serial_send */
osThreadId_t serial_sendHandle;
const osThreadAttr_t serial_send_attributes = {
  .name = "serial_send",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for led_control */
osThreadId_t led_controlHandle;
const osThreadAttr_t led_control_attributes = {
  .name = "led_control",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for gripper_control */
osThreadId_t gripper_controlHandle;
const osThreadAttr_t gripper_control_attributes = {
  .name = "gripper_control",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for adc_control */
osThreadId_t adc_controlHandle;
const osThreadAttr_t adc_control_attributes = {
  .name = "adc_control",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for button_press */
osThreadId_t button_pressHandle;
const osThreadAttr_t button_press_attributes = {
  .name = "button_press",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void Servo_Control(void *argument);
void OLED_Show(void *argument);
void Serial_Send(void *argument);
void LED_Control(void *argument);
void Gripper_Control(void *argument);
void ADC_Control(void *argument);
void Button_Press(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */
  evt_id_Serial = osEventFlagsNew(NULL);
  evt_id_LED = osEventFlagsNew(NULL);
	evt_id_OLED = osEventFlagsNew(NULL);
  evt_id_Servo_GR = osEventFlagsNew(NULL);
  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  // 创建舵机控制互斥�??
  xServoMutex = xSemaphoreCreateMutex();
  if (xServoMutex == NULL) {
    Error_Handler(); // 互斥锁创建失�??
  }
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  xOLEDQueue = xQueueCreate(10, sizeof(OLED_Message));
  xGripperQueue = xQueueCreate(10, sizeof(GripperCommand));
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of servo_control */
  servo_controlHandle = osThreadNew(Servo_Control, NULL, &servo_control_attributes);

  /* creation of oled_show */
  oled_showHandle = osThreadNew(OLED_Show, NULL, &oled_show_attributes);

  /* creation of serial_send */
  serial_sendHandle = osThreadNew(Serial_Send, NULL, &serial_send_attributes);

  /* creation of led_control */
  led_controlHandle = osThreadNew(LED_Control, NULL, &led_control_attributes);

  /* creation of gripper_control */
  gripper_controlHandle = osThreadNew(Gripper_Control, NULL, &gripper_control_attributes);

  /* creation of adc_control */
  adc_controlHandle = osThreadNew(ADC_Control, NULL, &adc_control_attributes);

  /* creation of button_press */
  button_pressHandle = osThreadNew(Button_Press, NULL, &button_press_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_Servo_Control */
/**
  * @brief  Function implementing the servo_control thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_Servo_Control */
void Servo_Control(void *argument)
{
  /* USER CODE BEGIN Servo_Control */
  /* Infinite loop */
  const TickType_t xFrequency = pdMS_TO_TICKS(20);  // 50Hz
  TickType_t xLastWakeTime = xTaskGetTickCount();
  for(;;)
  {

    if (Button_select_state == AXIS_ALL_ARMS) {
      if (xSemaphoreTake(xServoMutex, portMAX_DELAY) == pdTRUE) {
        Servo_Control_task();
        xSemaphoreGive(xServoMutex);
      }
    vTaskDelayUntil(&xLastWakeTime, xFrequency);  // 精确周期控制
    }
  }
  /* USER CODE END Servo_Control */
}

/* USER CODE BEGIN Header_OLED_Show */
/**
* @brief Function implementing the oled_show thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_OLED_Show */
void OLED_Show(void *argument)
{
  /* USER CODE BEGIN OLED_Show */
  /* Infinite loop */
  for(;;)
  {
    
    OLED_Show_task();
		
    //osDelay(1);
  }
  /* USER CODE END OLED_Show */
}

/* USER CODE BEGIN Header_Serial_Send */
/**
* @brief Function implementing the serial_send thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Serial_Send */
void Serial_Send(void *argument)
{
  /* USER CODE BEGIN Serial_Send */
  /* Infinite loop */
  for(;;)
  {
    Serial_Send_task();
    osDelay(1);
  }
  /* USER CODE END Serial_Send */
}

/* USER CODE BEGIN Header_LED_Control */
/**
* @brief Function implementing the led_control thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_LED_Control */
void LED_Control(void *argument)
{
  /* USER CODE BEGIN LED_Control */
  /* Infinite loop */
  for(;;)
  {
    LED_Control_task();
    osDelay(1);
  }
  /* USER CODE END LED_Control */
}

/* USER CODE BEGIN Header_Gripper_Control */
/**
* @brief Function implementing the gripper_control thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Gripper_Control */
void Gripper_Control(void *argument)
{
  /* USER CODE BEGIN Gripper_Control */
  /* Infinite loop */
  for(;;)
  {
    Gripper_Control_task();
    osDelay(1);
  }
  /* USER CODE END Gripper_Control */
}

/* USER CODE BEGIN Header_ADC_Control */
/**
* @brief Function implementing the adc_control thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_ADC_Control */
void ADC_Control(void *argument)
{
  /* USER CODE BEGIN ADC_Control */
  /* Infinite loop */
  for(;;)
  {
    if (Button_select_state != AXIS_ALL_ARMS) {
      if (xSemaphoreTake(xServoMutex, portMAX_DELAY) == pdTRUE) 
      {
        ADC_Control_task();
        xSemaphoreGive(xServoMutex);
      }
    }
    osDelay(10);
  }
  /* USER CODE END ADC_Control */
}

/* USER CODE BEGIN Header_Button_Press */
/**
* @brief Function implementing the button_press thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Button_Press */
void Button_Press(void *argument)
{
  /* USER CODE BEGIN Button_Press */
  /* Infinite loop */
  for(;;)
  {
    Button_Press_task();
    osDelay(20);
  }
  /* USER CODE END Button_Press */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

